package com.wuliu.action.app;

import com.wuliu.action.BaseAction;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.wuliu.model.order.Order;
import com.wuliu.model.order.OrderDetail;
import com.wuliu.util.BaseUtil;
import com.wuliu.util.DbUtil;
//验货，放货相关
public class AppSyncCheckAction extends BaseAction {
	/**
	 * 根据订单号查询订单列表
	 * @return 若干订单列表order+orderDetail
	 */
	public String getOrderListById(){
		String showMsg="";
		
		JSONObject jsonRet=new JSONObject();
		JSONArray jsonArray=new JSONArray();
		JSONObject jsonRet1 = new JSONObject();
		JSONObject json_detail = new JSONObject();
		JSONArray jsonArray_detail = new JSONArray();
		JSONArray jsona=new JSONArray();
		
		String id=BaseUtil.getStr(request.getParameter("id"));
		
		Order order=new Order();
		OrderDetail orderDetail=new OrderDetail();
		
		order.setCarId(id);
        orderDetail.setOrderId(id);
        
        if(id.equals("")){
        	showMsg="订单号不能为空";
        }else{
        	String sql="SELECT ID,SHIPPER_ID,SHIPPER_NAME,SHIPPER_TEL," +
				"CONSIGNEE_ID,CONSIGNEE_NAME,CONSIGNEE_TEL,CONSIGNEE_ADDRE," +
				"SENT_DATE,RECEIVE_DATE,CHECK_STATUS,ORDER_STATUS,COMPANY_ID," +
				"COMPANY_NAME,GOODS_NAME,FEE,TRANSFER_TYPE,PAY_TYPE," +
				"PAY_PEOPLE,FEE_ON_TRANSFER,TOTAL_FEE,IS_INSURE,IS_INSURANCE," +
				"CHECKER,CAR_ID,PAY_CODE,SHIPPER_ADDRE,PAY_FEE_TYPE,TOTAL_FEE_STATUS,SEND_TYPE FROM WL_ORDER WHERE ID='"+id+"'";
        	List<List<String>> list=DbUtil.getQueryList(sql, 30);
        	for(List<String> lt:list){
        		JSONObject json = new JSONObject();
    			json.put("id", lt.get(0));
    			json.put("shipperId", lt.get(1));
    			json.put("shipperName", lt.get(2));
    			json.put("shipperTel", lt.get(3));
    			json.put("consigneeId", lt.get(4));
    			json.put("consigneeName", lt.get(5));
    			json.put("consigneeTel", lt.get(6));
    			json.put("consigneeAddre", lt.get(7));
    			json.put("sentDate", lt.get(8));
    			json.put("receiveDate", lt.get(9));
    			json.put("checkStatus", lt.get(10));
    			json.put("orderStatus", lt.get(11));
    			json.put("companyId", lt.get(12));
    			json.put("companyName", lt.get(13));
    			json.put("goodsName", lt.get(14));
    			json.put("fee", lt.get(15));
    			json.put("transferType", lt.get(16));
    			json.put("payType", lt.get(17));
    			json.put("payPeople", lt.get(18));
    			json.put("feeOnTransfer", lt.get(19));
    			json.put("totalFee", lt.get(20));
    			json.put("isInsure", lt.get(21));
    			json.put("isInsurance", lt.get(22));
    			json.put("checker", lt.get(23));
    			json.put("carId", lt.get(24));
    			json.put("payCode", lt.get(25));
    			json.put("shipperAddre", lt.get(26));
    			json.put("payFeeType", lt.get(27));
    			json.put("totalFeeStatus", lt.get(28));
    			json.put("sendType", lt.get(29));
    			jsonArray.add(json);                    //orderlist
    			jsonRet1.put("order", json);
    			
    			
    			String sql_detail="SELECT ID,ORDER_ID,GOODS_NAME,GOODS_VOLUME," +
    					"GOODS_NUM,GOODS_WEIGHT,GOODS_FEE FROM WL_ORDER_DETAIL WHERE ORDER_ID= '"+lt.get(0)+"' ";
    			List<List<String>> list_detail = DbUtil.getQueryList(sql_detail, 7);
    			for(List<String> lt_detail:list_detail){
    				
    				json_detail.put("id", lt_detail.get(0));
    				json_detail.put("orderId", lt_detail.get(1));
    				json_detail.put("goodsName", lt_detail.get(2));
    				json_detail.put("goodsVolume", lt_detail.get(3));
    				json_detail.put("goodsNum", lt_detail.get(4));
    				json_detail.put("goodsWeight", lt_detail.get(5));
    				json_detail.put("goodsFee", lt_detail.get(6));
    				
    				jsonArray_detail.add(json_detail);                      //orderDetail
    				jsonRet1.put("orderDetail", jsonArray_detail);
        	}
    			jsona.add(jsonRet1);
    			jsonRet.put("orderList", jsona);
        }

		try {
			response.setCharacterEncoding("utf8");
			response.getWriter().print(jsonRet);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        }
		return null;
	}
	
	
	
	
	/**
	 * 列出所有待验单的物流单
	 * 请求传参：操作人员id
	 * @return  若干订单详情()
	 */
		public String GetUncheckOrderList(){
			String userAccount=request.getParameter("userAccount");
			String showMsg="";
			
			JSONObject jsonRet1 = new JSONObject();
			JSONObject jsonRet = new JSONObject();
			JSONObject json_detail = new JSONObject();
			JSONArray jsonArray = new JSONArray();
			JSONArray jsonArray_detail = new JSONArray();
			JSONArray jsona=new JSONArray();
			
			if(userAccount.equals("")){
				showMsg="账号不能为空！";
				jsonRet.put("showMsg", showMsg);
			}else{
			String sql="SELECT ID,SHIPPER_ID,SHIPPER_NAME,SHIPPER_TEL," +
				"CONSIGNEE_ID,CONSIGNEE_NAME,CONSIGNEE_TEL,CONSIGNEE_ADDRE," +
				"SENT_DATE,RECEIVE_DATE,CHECK_STATUS,ORDER_STATUS,COMPANY_ID," +
				"COMPANY_NAME,GOODS_NAME,FEE,TRANSFER_TYPE,PAY_TYPE," +
				"PAY_PEOPLE,FEE_ON_TRANSFER,TOTAL_FEE,IS_INSURE,IS_INSURANCE," +
				"CHECKER,CAR_ID,PAY_CODE,SHIPPER_ADDRE,PAY_FEE_TYPE,TOTAL_FEE_STATUS,SEND_TYPE FROM WL_ORDER WHERE ORDER_STATUS=0";
			List<List<String>> list = DbUtil.getQueryList(sql, 29);
			for(List<String>lt:list){
				JSONObject json=new JSONObject();
				json.put("id", lt.get(0));
				json.put("shipperId", lt.get(1));
				json.put("shipperName", lt.get(2));
				json.put("shipperTel", lt.get(3));
				json.put("consigneeId", lt.get(4));
				json.put("consigneeName", lt.get(5));
				json.put("consigneeTel", lt.get(6));
				json.put("consigneeAddre", lt.get(7));
				json.put("sentDate", lt.get(8));
				json.put("receiveDate", lt.get(9));
				json.put("checkStatus", lt.get(10));
				json.put("orderStatus", lt.get(11));
				json.put("companyId", lt.get(12));
				json.put("companyName", lt.get(13));
				json.put("goodsName", lt.get(14));
				json.put("fee", lt.get(15));
				json.put("transferType", lt.get(16));
				json.put("payType", lt.get(17));
				json.put("payPeople", lt.get(18));
				json.put("feeOnTransfer", lt.get(19));
				json.put("totalFee", lt.get(20));
				json.put("isInsure", lt.get(21));
				json.put("isInsurance", lt.get(22));
				json.put("checker", lt.get(23));
				json.put("carId", lt.get(24));
				json.put("payCode", lt.get(25));
				json.put("shipperAddre", lt.get(26));
				json.put("payFeeType", lt.get(27));
				json.put("totalFeeStatus", lt.get(28));
				json.put("sendType", lt.get(29));
				jsonArray.add(json);
				jsonRet1.put("order", json);
				
				String sql_detail="SELECT ID,ORDER_ID,GOODS_NAME,GOODS_VOLUME," +
						"GOODS_NUM,GOODS_WEIGHT,GOODS_FEE FROM WL_ORDER_DETAIL WHERE ORDER_ID= '"+lt.get(0)+"' ";
				List<List<String>> list_detail = DbUtil.getQueryList(sql_detail, 7);
				for(List<String> lt_detail:list_detail){
					
					json_detail.put("id", lt_detail.get(0));
					json_detail.put("orderId", lt_detail.get(1));
					json_detail.put("goodsName", lt_detail.get(2));
					json_detail.put("goodsVolume", lt_detail.get(3));
					json_detail.put("goodsNum", lt_detail.get(4));
					json_detail.put("goodsWeight", lt_detail.get(5));
					json_detail.put("goodsFee", lt_detail.get(6));
					
					jsonArray_detail.add(json_detail);
					jsonRet1.put("orderDetail", jsonArray_detail);
				
		    	}
//			jsonRet.put("userAccount", userAccount);
//			jsonRet.put("jsonArray", jsonArray);
				
				
				jsona.add(jsonRet1);
				
				jsonRet.put("orderList", jsona);
				
			}
			
			try {
				response.setCharacterEncoding("utf8");
				response.getWriter().print(jsonRet);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
			return null;
			
		}
		/**
		 * 列出某一时间段内的物流单默认为当天的物流单
		 * 请求传参：操作人员id
		 * @return 若干订单详情
		 */
		public String GetOrderListBySentDate(){
			JSONObject jsonRet=new JSONObject();
			JSONArray jsonArray=new JSONArray();
			
			String showMsg="";
			/*获取时间，并将时间转换成long类型*/
			String sentdate1=BaseUtil.getStr(request.getParameter("sentDate1"));
			String sentdate2=BaseUtil.getStr(request.getParameter("sentDate2"));
			
			Long date1=Long.valueOf(sentdate1);
			Long date2=Long.valueOf(sentdate2);
	        int len=(int) (date2-date1);
			for(int i=0;i<=len;i++){
				String sql="SELECT SHIPPER_ID,SHIPPER_NAME,SHIPPER_TEL WHERE SENT_DATE='"+String.valueOf(date1)+"'";
				date1=date1+i;
				List<List<String>> list=DbUtil.getQueryList(sql, 3);
				for(List<String> it:list){
					JSONObject json=new JSONObject();
					json.put("shipperId", it.get(0));
					json.put("shipperName", it.get(1));
					json.put("shipperTel", 2);
					jsonArray.add(json);
				}
				jsonRet.put("OrderList", jsonArray);
				
			}
			try {
				response.setCharacterEncoding("utf8");
				response.getWriter().print(jsonRet);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
		/**
		 * 对选中的订单进行验单操作
		 * 请求传参:操作人员id，订单id， 状态值（true  或者false,或者1和2）
		 * @return:正确或者错误
		 */
		public String UpdateCheckStatus(){
			JSONObject jsonRet=new JSONObject();
			JSONArray jsonArray=new JSONArray();
			
			String showMsg="";
			/*order表的处理*/
			String checker=BaseUtil.getStr(request.getParameter("checker"));    //验单员
			
			String id=BaseUtil.getStr(request.getParameter("id"));
			
			String fee=BaseUtil.getStr(request.getParameter("fee"));
			Order order=new Order();
			
			order.setId(id);
			order.setChecker(checker);
			order.setFee(fee);
			/*orderDetail表的处理*/
			String goodsNum=BaseUtil.getStr(request.getParameter("goodsNum"));
			String goodsVolume=BaseUtil.getStr(request.getParameter("goodsVolume"));
			String goodsWeight=BaseUtil.getStr(request.getParameter("goodsWeight"));
			OrderDetail orderDetail=new OrderDetail();
			orderDetail.setGoodsNum(goodsNum.equals("")?0:Integer.valueOf(goodsNum));
			orderDetail.setGoodsVolume(goodsVolume.equals("")? 0:Integer.valueOf(goodsVolume));
			orderDetail.setGoodsWeight(goodsWeight.equals("")?0:Double.valueOf(goodsWeight));
			
			
			if(id.equals("")){
				showMsg="订单号不能为空";
				jsonRet.put("showMsg", showMsg);
				
			}else{
				
				String sql="UPDATE WL_ORDER SET CHECK_STATUS=1,ORDER_STATUS=1 ,CHECKER=?,FEE=? WHERE ID=?";
				List<String> it=new ArrayList<String>();
				it.add(order.getChecker());
				it.add(order.getFee());
				it.add(order.getId());
				
				
				boolean result=DbUtil.excuSQL(it, sql);
				
				String sql2="UPDATE WL_ORDER_DETAIL SET GOODS_VOLUME=?,GOODS_NUM=?,GOODS_WEIGHT=? WHERE ORDER_ID=?";
				List<Object> it2=new ArrayList<Object>();
				it2.add(orderDetail.getGoodsVolume());
				it2.add(orderDetail.getGoodsNum());
				it2.add(orderDetail.getGoodsWeight());
				it2.add(order.getId());
				
				boolean result2=DbUtil.excuSQL2(it2, sql2);
				
				
				if(result&&result2){
					showMsg="验单成功";
				}else{
					showMsg="验单失败";
				}
				
			}
			jsonRet.put("showMsg", showMsg);
			
			try {
				response.setCharacterEncoding("utf8");
				response.getWriter().print(jsonRet);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			return null;
			
			
		}
		/**
		 * 列出未放货订单
		 * 
		 * @return
		 */
		public String getUnreceiptOrderList(){
			
			String userAccount=BaseUtil.getStr(request.getParameter("userAccount"));
			
			JSONObject jsonRet = new JSONObject();
			JSONArray jsonArray = new JSONArray();             
			JSONObject jsonRet1 = new JSONObject();
			JSONObject json_detail = new JSONObject();         
			JSONArray jsonArray_detail = new JSONArray();
			JSONArray jsona=new JSONArray();
			
			String sql = "SELECT ID,SHIPPER_ID,SHIPPER_NAME,SHIPPER_TEL," +
					"CONSIGNEE_ID,CONSIGNEE_NAME,CONSIGNEE_TEL,CONSIGNEE_ADDRE," +
					"SENT_DATE,RECEIVE_DATE,CHECK_STATUS,ORDER_STATUS,COMPANY_ID," +
					"COMPANY_NAME,GOODS_NAME,FEE,TRANSFER_TYPE,PAY_TYPE," +
					"PAY_PEOPLE,FEE_ON_TRANSFER,TOTAL_FEE,IS_INSURE,IS_INSURANCE," +
					"CHECKER,CAR_ID,PAY_CODE,SHIPPER_ADDRE,PAY_FEE_TYPE,TOTAL_FEE_STATUS,SEND_TYPE FROM WL_ORDER WHERE ORDER_STATUS=1";
			List<List<String>> list = DbUtil.getQueryList(sql, 29);
			for(List<String> lt:list){
				JSONObject json = new JSONObject();
				json.put("id", lt.get(0));
				json.put("shipperId", lt.get(1));
				json.put("shipperName", lt.get(2));
				json.put("shipperTel", lt.get(3));
				json.put("consigneeId", lt.get(4));
				json.put("consigneeName", lt.get(5));
				json.put("consigneeTel", lt.get(6));
				json.put("consigneeAddre", lt.get(7));
				json.put("sentDate", lt.get(8));
				json.put("receiveDate", lt.get(9));
				json.put("checkStatus", lt.get(10));
				json.put("orderStatus", lt.get(11));
				json.put("companyId", lt.get(12));
				json.put("companyName", lt.get(13));
				json.put("goodsName", lt.get(14));
				json.put("fee", lt.get(15));
				json.put("transferType", lt.get(16));
				json.put("payType", lt.get(17));
				json.put("payPeople", lt.get(18));
				json.put("feeOnTransfer", lt.get(19));
				json.put("totalFee", lt.get(20));
				json.put("isInsure", lt.get(21));
				json.put("isInsurance", lt.get(22));
				json.put("checker", lt.get(23));
				json.put("carId", lt.get(24));
				json.put("payCode", lt.get(25));
				json.put("shipperAddre", lt.get(26));
				json.put("payFeeType", lt.get(27));
				json.put("totalFeeStatus", lt.get(28));
				json.put("sendType", lt.get(29));
				jsonArray.add(json);
				jsonRet1.put("order", json);
				
				String sql_detail="SELECT ID,ORDER_ID,GOODS_NAME,GOODS_VOLUME," +
						"GOODS_NUM,GOODS_WEIGHT,GOODS_FEE FROM WL_ORDER_DETAIL WHERE ORDER_ID= '"+lt.get(0)+"' ";
				List<List<String>> list_detail = DbUtil.getQueryList(sql_detail, 7);
				for(List<String> lt_detail:list_detail){
					
					json_detail.put("id", lt_detail.get(0));
					json_detail.put("orderId", lt_detail.get(1));
					json_detail.put("goodsName", lt_detail.get(2));
					json_detail.put("goodsVolume", lt_detail.get(3));
					json_detail.put("goodsNum", lt_detail.get(4));
					json_detail.put("goodsWeight", lt_detail.get(5));
					json_detail.put("goodsFee", lt_detail.get(6));
					
					jsonArray_detail.add(json_detail);
					jsonRet1.put("orderDetail", jsonArray_detail);
				}
				
				jsona.add(jsonRet1);
				
				jsonRet.put("orderList", jsona);
				
			}

			try {
				response.setCharacterEncoding("utf8");
				response.getWriter().print(jsonRet);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return null;
			
		}
		
		/**
		 * 
		 * 验货
		 * @return
		 */
		public String CashReceipt(){
			String showMsg="";
			JSONObject jsonRet=new JSONObject();
			JSONArray jsonArray=new JSONArray();

			String orderId=BaseUtil.getStr(request.getParameter("Id"));
			String releasePeople=BaseUtil.getStr(request.getParameter("releasePeople"));
	        Order order=new Order();
	        order.setId(orderId);
			order.setReleasePeople(releasePeople);
			if(orderId.equals("")){
				showMsg="订单号不能为空";
			}else{
				
				/*处理订单表查询*/
				String sql1="SELECT SHIPPER_ID,SHIPPER_NAME,SHIPPER_TEL,CONSIGNEE_ID,CONSIGNEE_NAME,CONSIGNEE_TEL,"+
			" GOODS_NAME,TOTAL_FEE FROM WL_ORDER WHERE ID='"+orderId+"'";
			List<List<String>>list1=DbUtil.getQueryList(sql1, 8);
			for(List<String>it:list1){
				JSONObject json=new JSONObject();
				json.put("shipperId", it.get(0));
				json.put("shipperName", it.get(1));
				json.put("shipperTel", it.get(2));
				json.put("consigneeId",3 );
				json.put("consigneeName",4 );
				json.put("consigneeTel",5 );
				json.put("goodsName",6 );
				json.put("totalFee",7 );
				jsonArray.add(json);
			}
			
			/*处理订单表更改订单状态*/
			String sql3="UPDATE WL_ORDER SET ORDER_STATUS=2,TOTAL_FEE_STATUS=2,RELEASE_PEOPLE=?"
                		+"RECEIVE_DATE=unix_timestamp(now()) WHERE ID=?";
			List<String> it1=new ArrayList<String>();
			it1.add(order.getReleasePeople());
			it1.add(order.getId());
			boolean result=DbUtil.excuSQL(it1, sql3);
			/*处理货物详情表查询*/
			String sql2="SELECT GOODS_NAME,GOODS_VOLUME,GOODS_NUM,GOODS_WEIGHT FROM WL_ORDER_DETAIL WHERE ORDER_ID='"
					+orderId+"'";
			List<List<String>> list2=DbUtil.getQueryList(sql2, 4);
			for(List<String> it:list2){
				JSONObject json=new JSONObject();
				json.put("goodsName",it.get(0) );
				json.put("goodsVolume", it.get(1));
				json.put("goodsNum", it.get(2));
				json.put("goodsWeight", it.get(3));
				jsonArray.add(json);
			}
			
			if(result){
				showMsg="放货成功";
			}else{
				showMsg="放货失败";
			}
			jsonRet.put("showMsg", showMsg);
			//jsonRet.put("order", jsonArray);
			
			}
			

			try {
				response.setCharacterEncoding("utf8");
				response.getWriter().print(jsonRet);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
			
		}

}
